package com.valenx.distributedmulticamerasystem.utils;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;

//文件存储操作工具类
public class OperateStorageFileUtils {
    public enum MediaType{
        VIDEO,
        IMAGE,
        AUDIO,
        DOWNLOADS
    }
//=============================================================================================================

   //读取外部存储公共目录用的FD
    public static FileDescriptor getPublicFileFdForRead(Context context,MediaType type,String fileName){
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri externalDataAbilityUri = null;
        switch (type){
            case VIDEO:
                externalDataAbilityUri = AVStorage.Video.Media.EXTERNAL_DATA_ABILITY_URI;
                break;
            case IMAGE:
                externalDataAbilityUri = AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI;
                break;
            case DOWNLOADS:
                externalDataAbilityUri = AVStorage.Downloads.EXTERNAL_DATA_ABILITY_URI;
                break;
            case AUDIO:
                externalDataAbilityUri = AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI;
                break;
        }
        ResultSet rs = null;
        FileDescriptor fileDescriptor = null;
        try {
            DataAbilityPredicates predicates =
                    new DataAbilityPredicates("_display_name = '" + fileName + "'");
            rs = helper.query(externalDataAbilityUri,new String[]{AVStorage.Video.Media.ID},predicates);
            System.out.println("rs count:" + rs.getRowCount());
            if(rs != null){
                while (rs.goToNextRow()) {
                    int mediaId = rs.getInt(rs.getColumnIndexForName(AVStorage.Video.Media.ID));
                    Uri uri = Uri.appendEncodedPathToUri(externalDataAbilityUri,"" + mediaId);
                    fileDescriptor = helper.openFile(uri, "r");
                    return fileDescriptor;
                }
            }
        } catch (Exception e) {
            System.out.println("rs read error!");
            e.printStackTrace();
        } finally {
            if (rs != null) {
                rs.close();
            }
        }
        return fileDescriptor;
    }
//=============================================================================================================

    //存储外部存储公共目录用的FD
    public static FileDescriptor getPublicFdForInsert(Context context,
                                                      MediaType type, String name) {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri externalDataAbilityUri = null;
        ValuesBucket vb = new ValuesBucket();
        switch (type){
            case VIDEO:
                externalDataAbilityUri = AVStorage.Video.Media.EXTERNAL_DATA_ABILITY_URI;
                vb.putString(AVStorage.Video.Media.DISPLAY_NAME,name);
                break;
            case IMAGE:
                externalDataAbilityUri = AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI;
                vb.putString(AVStorage.Images.Media.DISPLAY_NAME,name);
                break;
            case DOWNLOADS:
                externalDataAbilityUri = AVStorage.Downloads.EXTERNAL_DATA_ABILITY_URI;
                vb.putString(AVStorage.Downloads.DISPLAY_NAME,name);
                break;
            case AUDIO:
                externalDataAbilityUri = AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI;//目录URI
                vb.putString(AVStorage.Audio.Media.DISPLAY_NAME,name);
                break;
        }
        FileOutputStream outputStream = null;
        FileDescriptor fd = null;
        try {
            int id = helper.insert(externalDataAbilityUri, vb);
            System.out.println("insert rs:" + id);
            Uri uri = Uri.appendEncodedPathToUri(externalDataAbilityUri, ""+id); //文件URI
            outputStream = (FileOutputStream) helper.obtainOutputStream(uri);
            fd = outputStream.getFD();
            return fd;
        } catch (Exception e) {
            System.out.println(" helper.insert error！");
            e.printStackTrace();
        }finally {
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fd;
    }
}
